package com.googlecode.stockhamster.stockstat;

import com.googlecode.stockhamster.Constant;

public class StockStat implements IStockStat {
	@Override
	public Boolean isValid(Integer stockId) {
		Stock stock = Stock.get(stockId);
		return stock != null && !stock.isLimited();
	}

	@Override
	public Boolean updatePrice(String code, Integer stockId, Long price,
			Long volumes) {
		if (!code.equals(Constant.statusAdminCode))
			return false;
		Stock stock = Stock.get(stockId);
		if (stock == null)
			return false;
		stock.setPrice(price);
		if (!stock.save())
			return false;
		return StockHistory.stockChange(stockId, price, volumes);
	}

	@Override
	public Long getPrice(Integer stockId) {
		Stock stock = Stock.get(stockId);
		if (stock == null)
			return null;
		return stock.getPrice();
	}

	@Override
	public String getName(Integer stockId) {
		Stock stock = Stock.get(stockId);
		if (stock == null)
			return null;
		return stock.getName();
	}

	@Override
	public Integer[] getStocks() {
		Stock[] stocks = Stock.getAll();
		Integer[] result = new Integer[stocks.length];
		for (int i = 0; i < stocks.length; i++)
			result[i] = stocks[i].getId();
		return result;
	}

	@Override
	public String getCategory(Integer stockId) {
		Stock stock = Stock.get(stockId);
		if (stock == null)
			return null;
		return stock.getCategory();
	}

	@Override
	public Integer addStock(String code, String name, String category,
			Long price) {
		if (!code.equals(Constant.statusAdminCode))
			return null;
		Stock stock = new Stock();
		stock.setName(name);
		stock.setCategory(category);
		stock.setPrice(price);
		if (!stock.insert())
			return null;
		StockHistory.stockInit(stock.getId(), price);
		return stock.getId();
	}

	@Override
	public Boolean delStock(String code, Integer stockId) {
		if (!code.equals(Constant.statusAdminCode))
			return false;
		Stock stock = Stock.get(stockId);
		if (stock == null)
			return false;
		return stock.delete();
	}

	@Override
	public Boolean setStockLimited(String code, Integer stockId, Boolean limited) {
		if (!code.equals(Constant.statusAdminCode))
			return false;
		Stock stock = Stock.get(stockId);
		stock.setLimited(limited);
		return stock.save();
	}

	@Override
	public Long[] getHistory(Integer stockId) {
		Long[] res = new Long[1024];
		StockHistory.refresh(stockId);
		StockHistory[] historys = StockHistory.getHistorys(stockId, 1024);
		int i;
		for (i = 0; i < historys.length; i++)
			res[i] = historys[i].getPrice();
		for (; i < 1024; i++)
			res[i] = historys[historys.length - 1].getPrice();
		return res;
	}

	@Override
	public Integer[] getCategoryStocks(String category) {
		Stock[] stocks = Stock.getAll(category);
		Integer[] stockIds = new Integer[stocks.length];
		for (int i = 0; i < stocks.length; i++)
			stockIds[i] = stocks[i].getId();
		return stockIds;
	}

	@Override
	public Integer getStockId(String stockName) {
		Stock s = Stock.get(stockName);
		if (s == null)
			return null;
		return s.getId();
	}
}
